<?php
/**
 * Title:Core Class Base
 * Times:2010.04.16
 * Author:雅典娜之子
 */
!defined('ACCESSING') && exit('Forbidden');

class Base
{
	var $db;
	var $base;
    var $member;
    /**
     * 構造方法
     */
    public function  __construct()
    {
		global $db,$BASE;
		$this->db = $db;
		$this->base = $BASE;
        include_once $this->base['corepath'].'member.class.php';
        $this->member = new Member();
    }

    //PHP4構造方法
    public function Base()
    {
        $this->__construct();
    }

    protected function imperfect($msg, $url, $msgtype)
    {
        ShowMsg($msg, $url, $msgtype);
        exit();
    }

    /**
     * 包含模版文件渲染視圖層
     * 
     * @param string $pagename 包含進來的文件名，不含副檔名
     * @param array $assign 向模版文件渲染的變量數組
     *
     */
    protected function display($pagename='',$assign='0')
    {
		$pagename == '' && die('解析文件名不能為空！');
        if($assign != '0' && is_array($assign))
        {
            $assArray = array();
            foreach($assign as $key=>$value)
            {
                $assArray[$key] = $value;
            }
        }

		$TempPageName = $pagename.".tpl.php";

		!file_exists($TempPageName) && die('解析文件不存在！');
        require $TempPageName;
    }

    protected function GetPostStr($key,$gtype='all')
    {
		if($gtype == 'all')
		{
			if(!empty($_GET) && !empty($_GET[$key]))
			{
				return $this->splacestr(trim($_GET[$key]));
			}
			elseif(!empty($_POST) && !empty($_POST[$key]))
			{
				return $this->splacestr(trim($_POST[$key]));
			}
		}
		elseif($gtype=='get')
		{
			$this->GetStr($key);
		}
		elseif ($gtype=='post') 
		{
			$this->PostStr($key);
		}
		else 
		{
			return null;
		}
    }

	protected function GetStr($key)
	{
		if(!empty($_GET) && !empty($_GET[$key]))
		{
			return $this->splacestr(trim($_GET[$key]));
		}
		else 
		{
			return null;
		}
	}

	protected function PostStr($key)
	{
		if(!empty($_POST) && !empty($_POST[$key]))
		{
			return $this->splacestr(trim($_POST[$key]));
		}
		else 
		{
			return null;
		}
	}

    public function getPost()
    {
        $postArray = $_POST;
        if(!get_magic_quotes_gpc()) {
            if(is_array($postArray)) {
                foreach($postArray as $key=>$value) {
                    $postArray[$key] = $this->splacestr($postArray[$key]);
                    $postArray[$key] = addslashes($value);
                    if(empty($postArray[$key]) || $postArray[$key] == '')
                    {
                        $postArray[$key] = '';
                    }
                }
            }
        }
        return $postArray;
    }

    protected function splacestr($str)
    {
        $dArray = array("'"," or "," and ","*","select ","insert ","?","=","<",">");
        foreach ($dArray as $k=>$v)
        {
            $str = str_replace($v, '', $str);
        }
        return trim($str);
    }

	protected function JumpUrl($url='index.php',$jtype=1)
	{
		switch($jtype)
		{
			case 1:
				echo "<script>window.location.href=('".$url."');</script>";
				break;
			case 2:
				echo "<script>parent.location.href=('".$url."');</script>";
				break;
		}
	}

	protected function GetUnixTime($dtime)
	{ 
	 	if(!ereg("[^0-9]",$dtime)) {return $dtime;} 
			$dt = Array(1970,1,1,0,0,0); 
			$dtime = ereg_replace("[\r\n\t]|日|秒"," ",$dtime);
			$dtime = str_replace("年","-",$dtime);
			$dtime = str_replace("月","-",$dtime);
			$dtime = str_replace("時",":",$dtime);
			$dtime = str_replace("分",":",$dtime);
            $dtime = str_replace("/","-",$dtime);
			$dtime = trim(ereg_replace("[ ]{1,}"," ",$dtime));
			$ds = explode(" ",$dtime);
			$ymd = explode("-",$ds[0]);
			if(isset($ymd[0])) $dt[0] = $ymd[0];
			if(isset($ymd[1])) $dt[1] = $ymd[1];
			if(isset($ymd[2])) $dt[2] = $ymd[2];
			if(strlen($dt[0])==2) $dt[0] = '20'.$dt[0];
			if(isset($ds[1])){ $hms = explode(":",$ds[1]);
			if(isset($hms[0])) $dt[3] = $hms[0];
			if(isset($hms[1])) $dt[4] = $hms[1];
			if(isset($hms[2])) $dt[5] = $hms[2];
		} 
		foreach($dt as $k=>$v)
		{ 
			$v = ereg_replace("^0{1,}","",trim($v));
			if($v=="") $dt[$k] = 0;
		}
		$mt = mktime($dt[3],$dt[4],$dt[5],$dt[1],$dt[2],$dt[0]);
		if($mt>0) return $mt; else return mytime();
	} 

	protected function PageNow() 
	{
		if(!empty($_GET['page']))
		{
			$PageNow = $this->GetStr('page');
		}
		else
		{
			$PageNow = 1;
		}

		!is_numeric($PageNow) && $PageNow = 1;
		$PageNow = intval($PageNow);
		$PageNow < 1 && $PageNow = 1;
		return $PageNow;
	}

    protected function GetSiteInfo()
    {
        $result = $this->db->query("SELECT * FROM `jm_setting`");
        while($row = $this->db->fetch_array($result))
        {
            $info[] = $row;
        }
        if(is_array($info))
            return $info;
        else
            return null;
    }

    protected function updateSetting($setting)
    {
        if(is_array($setting))
        {
            foreach($setting as $key=>$value)
            {
                $this->db->query("UPDATE `jm_setting` SET svalue = '$value' WHERE sname = '$key'");
            }
            return true;
        }
    }
}

?>
